package com.ruoyi.web.controller.community;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.BbsArticleDetail;
import com.ruoyi.community.domain.BbsFavorite;
import com.ruoyi.community.mapper.BbsArticleDetailMapper;
import com.ruoyi.community.service.BbsArticleService;
import com.ruoyi.community.service.IBbsFavoriteService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 论坛收藏Controller
 * 
 * @author Gaotian
 * @date 2021-12-11
 */
@RestController
@RequestMapping("/bbs/favorite")
public class BbsFavoriteController extends BaseController
{
    @Autowired
    private IBbsFavoriteService bbsFavoriteService;
    @Autowired
    private BbsArticleService bbsArticleService;
    /**
     * 查询论坛收藏列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BbsFavorite bbsFavorite)
    {
        startPage();
        List<BbsFavorite> list = bbsFavoriteService.selectBbsFavoriteList(bbsFavorite);
        return getDataTable(list);
    }

    /**
     * 查询文章是否在用户收藏列表中
     */
    @PostMapping("/isFa")
    public int isFa(@RequestBody BbsFavorite bbsFavorite)
    {
        bbsFavorite.setUserid(SecurityUtils.getUserID());
        System.out.println(bbsFavorite);
        List<BbsFavorite> list = bbsFavoriteService.selectBbsFavoriteList(bbsFavorite);
        return list.size()>0?1:0;
    }
    /**
     * 导出论坛收藏列表
     */
    @Log(title = "论坛收藏", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BbsFavorite bbsFavorite)
    {
        List<BbsFavorite> list = bbsFavoriteService.selectBbsFavoriteList(bbsFavorite);
        ExcelUtil<BbsFavorite> util = new ExcelUtil<BbsFavorite>(BbsFavorite.class);
        return util.exportExcel(list, "论坛收藏数据");
    }

    /**
     * 获取论坛收藏详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bbsFavoriteService.selectBbsFavoriteById(id));
    }

    /**
     *   新增论坛收藏
     */
    @Log(title = "论坛收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BbsFavorite bbsFavorite)
    {
        bbsFavorite.setUserid(SecurityUtils.getUserID());
        bbsFavorite.setCommunityid(SecurityUtils.getCommunityId());
        if(isFa(bbsFavorite)==0) {
            return toAjax(bbsFavoriteService.insertBbsFavorite(bbsFavorite));
        }
        else{
            AjaxResult ajax = new AjaxResult();
            return ajax.error(300,"您已收藏过");
        }
    }

    /**
     * 修改论坛收藏
     */
    @Log(title = "论坛收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BbsFavorite bbsFavorite)
    {
        return toAjax(bbsFavoriteService.updateBbsFavorite(bbsFavorite));
    }

    /**
     * 删除论坛收藏
     */
    @Log(title = "论坛收藏", businessType = BusinessType.DELETE)
	@PostMapping("/remote")
    public AjaxResult remove(@RequestBody BbsFavorite bbsFavorite)
    {
        bbsFavorite.setUserid(SecurityUtils.getUserID());
        bbsFavorite.setCommunityid(SecurityUtils.getCommunityId());
        if(isFa(bbsFavorite)==0) {
            AjaxResult ajax = new AjaxResult();
            return ajax.error(300,"您已取消收藏");
        }
        else{
            return toAjax(bbsFavoriteService.deleteBbsFavorite(bbsFavorite));
        }
    }

    /**
     *    获取用户收藏的文章列表
     */
    @GetMapping("/favoritelist")
    public TableDataInfo getList(){
        Long userid = SecurityUtils.getUserID();
        Integer communityid = SecurityUtils.getCommunityId();
        startPage();
        List<BbsArticleDetail> list = bbsArticleService.selectFavoriteArticleDetailList(userid,communityid);
        return getDataTable(list);
    }
}
